package com.learn.service.impl;

import com.learn.mapper.AccountMapper;
import com.learn.pojo.Account;
import com.learn.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author DJJ_F
 * @version 1.0
 * @date 2025/01/19
 * @description
 */
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public void transfer(int fromId, int toId, double money) {
        Account from = accountMapper.selectById(fromId);
        Account to = accountMapper.selectById(toId);
        if (from.getBalance() < money) {
            throw new RuntimeException("余额不足");
        }
        from.setBalance(from.getBalance() - money);
        to.setBalance(to.getBalance() + money);

        int count = accountMapper.updateBalance(from);
        String s = null;
        s.toString();
        count += accountMapper.updateBalance(to);
        if (count != 2) {
            throw new RuntimeException("系统异常");
        }
    }
    @Override
    public int save(Account account) {
        return accountMapper.insert(account);
    }

    @Override
    public int deleteById(Integer id) {
        return accountMapper.deleteById(id);
    }

    @Override
    public List<Account> getAll() {
        return accountMapper.selectAll();
    }

    @Override
    public Account getById(Integer id) {
        return accountMapper.selectById(id);
    }
}
